import Taro, { Component } from '@tarojs/taro'
import { View, Text, Swiper, SwiperItem, Image } from '@tarojs/components'
import './firstEntry.css'
import { AtGrid, AtButton, AtTabBar, AtIcon,AtList, AtListItem } from 'taro-ui'
import app from '../../../main.js'

export default class FirstEntry extends Component {

    config = {
        navigationBarTitleText: '首页',
    }

    constructor() {
        super(...arguments);
        this.state = {
            advertisements:[],
            tbar_current: 0,
        };
    }
    

    componentDidShow() {
        this.allAdvertisement()
    }

    allAdvertisement() {
        var me = this;
        var data = {code: '100'};
        app.get("web/advertisement/banners", data, function(res) {
            var data1 = [];
            for(var i = 0; i < res.data.result.length; i++) {
                var m = {
                    id: res.data.result[i].id,
                    position_id: res.data.result[i].position_id,
                    image: app.getPath(res.data.result[i].image),
                    title: res.data.result[i].title,
                    url: res.data.result[i].url,
                    sort: res.data.result[i].sort,
                }
                data1.push(m);
            }
            me.setState({
                advertisements:data1
            });      
        });
    }

    handleClick (value) {
        this.setState({
            tbar_current: value
        })
        if (value == 1 || value ==2) {
            this.authorize();
        }
    }

    authorize(){
      let isLogin = (app.isEmpty(app.current.head_image) || app.isEmpty(app.current.phone))
              Taro.showModal({
          title: isLogin ? '登陆提示' : '认证提示',
                content: isLogin ? '登陆之后可查看详细内容，是否微信授权登陆' : '您还未提交认证材料，请先填写并提交认证材料',
            }).then(res=>{
              if (app.isEmpty(app.current)) {
                app.current = {}
              }
              if (app.isEmpty(app.current.auth_status)) {
                app.current.auth_status = '未认证';
              }
              let auth_status = app.current.auth_status
              console.log(app.current);
              if(res.confirm){
                  if(app.isEmpty(app.current.head_image)){
                      app.navigateTo('/pages/doctorpages/authlogin/authlogin')
                  }else if(app.isEmpty(app.current.phone)){
                      app.navigateTo('/pages/doctorpages/authphone/authphone')
                  } else {
                    if (auth_status == '未认证') { //跳转去填写资料
                      app.redirectTo('/pages/center/pages/doctorcertified/doctorcertified');
                    } else if (auth_status == '申请中' || auth_status == '审核不通过') {
                      app.redirectTo('/pages/center/pages/doctorstatus/doctorstatus');
                    } else {
                      app.redirectTo('/pages/doctorpages/homepage/homepage');
                    }
                  }
              }else{
                  this.setState({
                      tbar_current: 0
                  })
              }
        })
    }

    render() {
        return (
        <View className='home-page'>
            <Swiper
                className='the_swiper'
                indicatorColor='#999'
                indicatorActiveColor='white'
                circular
                indicatorDots
                autoplay>
                {this.state.advertisements.map((item, index) =>
                    <SwiperItem key={index}>
                    <View ><Image className='banner_img' src={item.image} onClick={this.authorize.bind(this)}></Image></View>
                    </SwiperItem>
                )}
            </Swiper>
            <View className='doctor'>
                <View className='left'>
                    <Image className='head_img' onClick={this.authorize.bind(this)}></Image>
                    <View className='welcome' onClick={this.authorize.bind(this)}>请先登录</View>
                </View>
                <View className='to_btn'><AtButton type='primary' size='small' onClick={this.authorize.bind(this)}>我的名片</AtButton></View>
            </View>
            <View className='gridParent'>
                <AtGrid className='main-box' onClick={this.authorize.bind(this)} columnNum={4} data={
                    [
                    {
                        image: 'https://img20.360buyimg.com/jdphoto/s72x72_jfs/t15151/308/1012305375/2300/536ee6ef/5a411466N040a074b.png',
                        value: '病历管理'
                    },
                    {
                        image: 'https://img10.360buyimg.com/jdphoto/s72x72_jfs/t5872/209/5240187906/2872/8fa98cd/595c3b2aN4155b931.png',
                        value: '患者咨询'
                    },
                    {
                        image: 'https://img30.360buyimg.com/jdphoto/s72x72_jfs/t5770/97/5184449507/2423/294d5f95/595c3b4dNbc6bc95d.png',
                        value: '随访患者'
                    },
                    {
                        image: 'https://img30.360buyimg.com/jdphoto/s72x72_jfs/t5770/97/5184449507/2423/294d5f95/595c3b4dNbc6bc95d.png',
                        value: 'PRL订单'
                    },
                    ]
                } />
            </View>
            <AtTabBar
                fixed
                tabList={[
                    { title: '首页', iconType: 'home' },
                    { title: '动态', iconType: 'message'},
                    { title: '我的', iconType: 'user' }
                ]}
                onClick={this.handleClick.bind(this)}
                current={this.state.tbar_current}
                /> 
        </View>
        )
    }
}

